package chances.epg.data.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.lang.StringUtils;

import chances.epg.entity.biz.CornerMark;
import chances.epg.entity.content.ContentBaseTag;
import chances.epg.entity.content.ContentOpTag;
import chances.epg.entity.content.ContentType;

public class BaseDataRepertory {
	private static BaseDataRepertory instance;
	private List<CornerMark> cornerMarkList;
	private Map<String,CornerMark> cornerMarkMap;
	private Map<String,String> typeList;
	private Map<String,String> baseTagList;
	private Map<String,String> opTagList;
	
	
	protected BaseDataRepertory() {
		cornerMarkList = new ArrayList<CornerMark>();
		typeList = new ConcurrentHashMap<String,String>();
		baseTagList = new ConcurrentHashMap<String,String>();
		opTagList = new ConcurrentHashMap<String,String>();
		cornerMarkMap = new ConcurrentHashMap<String,CornerMark>();
	}
	
	public static BaseDataRepertory getInstance() {
		if(instance == null) {
			instance = new BaseDataRepertory();
		}
		
		return instance;
	}
	
	public void initCornerMarks(List<CornerMark> allMarks) {
		List<CornerMark> old = this.cornerMarkList;
		this.cornerMarkList = allMarks;
		old.clear();
		old = null;
		
		//added by libf
		Map<String,CornerMark> result = new ConcurrentHashMap<String,CornerMark>();
		for (CornerMark cornerMark : allMarks) {
			result.put(cornerMark.getCode(), cornerMark);
		}
		Map<String,CornerMark> oldMaps = this.cornerMarkMap;
		this.cornerMarkMap = result;
		oldMaps.clear();
		oldMaps = null;
	}
	
	public void initTypes(List<ContentType> allTypes) {
		Map<String,String> result = new ConcurrentHashMap<String,String>();
		for (ContentType contentType : allTypes) {
			result.put(contentType.getCode(), contentType.getName());
		}
		
		Map<String,String> oldMaps = this.typeList;
		this.typeList = result;
		oldMaps.clear();
		oldMaps = null;
	}
	
	public void initBaseTags(List<ContentBaseTag> allTags) {
		Map<String,String> result = new ConcurrentHashMap<String,String>();
		for (ContentBaseTag tag : allTags) {
			result.put(tag.getCode(), tag.getName());
		}
		
		Map<String,String> oldMaps = this.baseTagList;
		this.baseTagList = result;
		oldMaps.clear();
		oldMaps = null;
	}
	
	public void initOpTags(List<ContentOpTag> allTags) {
		Map<String,String> result = new ConcurrentHashMap<String,String>();
		for (ContentOpTag tag : allTags) {
			result.put(tag.getCode(), tag.getName());
		}
		
		Map<String,String> oldMaps = this.opTagList;
		this.opTagList = result;
		oldMaps.clear();
		oldMaps = null;
	}
	
	public String getTypeName(String typeCode) {
		if(StringUtils.isBlank(typeCode)){
			return "";
		}
		
		String name = this.typeList.get(typeCode);
		if(StringUtils.isNotBlank(name)){
			return name;
		}else{
			return "";
		}
	}
	
	public String getBaseTagName(String baseTagCode) {
		if(StringUtils.isBlank(baseTagCode)){
			return "";
		}
		
		String name = this.baseTagList.get(baseTagCode);
		if(StringUtils.isNotBlank(name)){
			return name;
		}else{
			return "";
		}
	}
	
	public String getOpTagName(String opTagCode) {
		if(StringUtils.isBlank(opTagCode)){
			return "";
		}
		
		String name = this.opTagList.get(opTagCode);
		if(StringUtils.isNotBlank(name)){
			return name;
		}else{
			return "";
		}
	}

	public CornerMark getCornerMark(String code) {
	    return this.cornerMarkMap.get(code);	
	}
	
	public List<CornerMark> getCornerMarkList() {
		return cornerMarkList;
	}

	public Map<String, String> getTypeList() {
		return typeList;
	}

	public Map<String, String> getBaseTagList() {
		return baseTagList;
	}

	public Map<String, String> getOpTagList() {
		return opTagList;
	}
}
